<template>
	<view class="container">
		<!--列表.开始-->
		<view class="group-box">
			<uni-swipe-action class="favorite-list">
				<!--循环.开始-->
				<view v-for="(item,index) in listData" :key="index" class="list-box">
					<uni-swipe-action-item>
						<navigator 
							:url="'../../goods/show?id='+item.goodsId"
							class="item-box">
							<view class="img-box">
								<image mode="widthFix" :src="$api.loadFile(item.imgUrl)"></image>
							</view>
							<view class="txt-box">
								<text class="title">{{item.title}}</text>
								<text class="time iconfont icon-time">{{item.addTime}}</text>
							</view>
						</navigator>
						<template v-slot:right>
							<view @click="deleteItem(item.id)" class="list-btn">
								<text class="icon iconfont icon-delete"></text>
							</view>
						</template>
					</uni-swipe-action-item>
				</view>
				<!--循环.结束-->
			</uni-swipe-action>
			<uni-load-more :status="loadingType" @clickLoadMore="loadData('add')"></uni-load-more>
		</view>
		<!--列表.结束-->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loadingType: "more", //加载状态
				totalCount: 0, //总数量
				totalPages: 0, //总页数
				pageSize: 10, //每页显示条数
				pageIndex: 1, //当前页码
				listData: [], //数据列表
			}
		},
		methods: {
			//初始化数据,带下滑刷新和上滑加载
			async loadData(type='add', loading){
				let _this = this;
				//上滑加载更多
				if(type === 'add'){
					if(_this.loadingType === 'nomore'){
						return;
					}
					_this.loadingType = 'loading';
				}
				//下滑刷新
				else{
					_this.pageIndex=1;
					_this.listData=[];
					_this.loadingType="more";
				}
				
				//加载收藏列表
				await _this.$api.request({
					url: `/account/member/favorite?pageSize=${_this.pageSize}&pageIndex=${_this.pageIndex}`,
					loading: true
				}).then(res=>{
					let pageInfo = JSON.parse(res.header["x-pagination"]);
					_this.totalCount = pageInfo.totalCount;
					_this.totalPages = pageInfo.totalPages;
					//console.log(res.data)
					_this.listData = _this.listData.concat(res.data);
				}).catch((err)=>{
					_this.loadingType="nomore";
					uni.stopPullDownRefresh();
				});
				
				//判断是否还有下一页
				if(_this.pageIndex >= _this.totalPages){
					_this.loadingType="nomore";
				}else{
					_this.pageIndex++;
					_this.loadingType="more";
				}
				//下滑隐藏动画及上滑事件
				if(type === 'refresh'){
					if(loading == 1){
						uni.hideLoading()
					}else{
						uni.stopPullDownRefresh();
					}
				}
			},
			//删除数据
			async deleteItem(id){
				let _this=this;
				await _this.$api.request({
					url: `/account/member/favorite/${id}`,
					method: "delete",
					load: true
				}).then(res=>{
					this.loadData('refresh');
				});
			}
		},
		//页面初始化
		onLoad(options){
			this.loadData('add');
		},
		//下滑刷新
		onPullDownRefresh(){
			this.loadData('refresh');
		},
		//上滑加载更多
		onReachBottom(){
			this.loadData('add');
		}
	}
</script>

<style lang="scss">
	.favorite-list{
		.list-box{
			margin-bottom: 24rpx;
			background: #fff;
			border-radius: 20rpx;
			overflow: hidden;
			.list-btn{
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 30rpx;
				background: #ff0000;
				.icon{
					color: #fff;
					font-size: 40rpx;
				}
			}
			.item-box{
				display: flex;
				justify-content: space-between;
				padding: 24rpx;
				.img-box{
					flex-shrink: 0;
					display: flex;
					align-items: center;
					box-sizing: border-box;
					padding: 2rpx;
					width: 168rpx;
					height: 168rpx;
					border-radius: 20rpx;
					background: #f5f5f5;
					image{
						border-radius: 20rpx;
						width: 100%;
					}
				}
				.txt-box{
					flex-grow: 1;
					display: flex;
					flex-direction: column;
					margin-left: 24rpx;
					.title{
						display: block;
						color: #333;
						font-size: 30rpx;
						line-height: 48rpx;
						text-overflow: -o-ellipsis-lastline;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						line-clamp: 2;
						-webkit-box-orient: vertical;
					}
					.time{
						margin-top: 24rpx;
						color: #999;
						font-size: 24rpx;
						&::before{
							margin-right: 6rpx;
						}
					}
				}
			}
		}
	}
</style>
